package com.ey12.service;

import java.util.List;

import com.ey12.db.dao.GmAccessDAO;
import com.ey12.db.dao.GmCserverDAO;
import com.ey12.db.dao.GmGameDAO;
import com.ey12.db.domain.GmAccess;
import com.ey12.db.domain.GmCserver;
import com.ey12.db.domain.GmGame;
import com.ey12.db.domain.GmUser;

public class GmAccessService {
	private GmAccessDAO gmAccessDAO;
	private GmGameDAO gmGameDAO;
	private GmCserverDAO gmCserverDAO;
	public void setGmAccessDAO(GmAccessDAO gmAccessDAO) {
		this.gmAccessDAO = gmAccessDAO;
	}

	public GmAccessDAO getGmAccessDAO() {
		return gmAccessDAO;
	}
	
	public void saveUserState(GmAccess gm){
		gmAccessDAO.saveOrUpdate(gm);
	}
	
	public List getSellViewFirstCbx(String username) {
//		System.out.println("getSellViewFirstCbx() used");
		List list = gmAccessDAO.findSellViewFirstCbx(username);
//		System.out.println(list.size());
		if (list.size() > 0) {
//			System.out.println("getSellViewFirstCbx()="+list.size());
			return list;
		}
		return null;
	}
	public List getFirstCbx(String userName) {
		List list = gmAccessDAO.findFirstCbx(userName);
		if (list.size() > 0) {
			list.add(0, "");
			return list;
		}
		return null;
	}
	
	public List getSellViewSecondCbx(String username,String gameName) {
//		System.out.println("getSellViewSecondCbx() used");
		List games = gmAccessDAO.findSellViewSecondCbx(username,gameName);
		if (games.size() > 0) {
//			System.out.println("getSellViewSecondCbx()="+games.size());
			return games;
		}
		return null;
	}
	public String addAccess(GmUser user,String gameName){
		List games=gmAccessDAO.findAccess(user,gameName);
			if(games.size()==0){
			GmAccess newAccess = new GmAccess();
			newAccess.setGmUser(user);
	//		List list1 = gmCserverDAO.defineUnit(gameName, cserverName, serverType, unit, camp);
	//		if(list1.size()>0){
	//			GmCserver cserver =(GmCserver)list1.get(0);
	//			newAccess.setGmCserver(cserver);
	//		}
			List list2 = gmGameDAO.findByGameName(gameName);
			if (list2.size() > 0) {
				GmGame game =(GmGame)list2.get(0);
				newAccess.setGmGame(game);
			}
			newAccess.setIsChecked(new Integer(1));
			newAccess.setId(null);
			gmAccessDAO.saveOrUpdate(newAccess);
			return "success";
		}
			return null;
	}
	
	public List getAccessByName(String username){
		List list = gmAccessDAO.findAccessByName(username);
		if(list.size()>0){
			return list;
		}
		return null;
	}
	
	public List getAccess(){
		List list = gmAccessDAO.findAllAccess();
		if(list.size()>0){
			return list;
		}
		return null;
	}

	public void saveAccesss(GmAccess access){
		gmAccessDAO.saveOrUpdate(access);
	}
	public void deleteAccess(GmAccess access){
		gmAccessDAO.delete(access);
	}
	public void setGmGameDAO(GmGameDAO gmGameDAO) {
		this.gmGameDAO = gmGameDAO;
	}

	public GmGameDAO getGmGameDAO() {
		return gmGameDAO;
	}

	public void setGmCserverDAO(GmCserverDAO gmCserverDAO) {
		this.gmCserverDAO = gmCserverDAO;
	}

	public GmCserverDAO getGmCserverDAO() {
		return gmCserverDAO;
	}
}
